package com.wolfword.crawler.server.scheduler;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.wolfword.crawler.server.service.CrawlTaskService;

@Component
@EnableScheduling 
public class NewTaskReceiver {

	@Autowired
	private CrawlTaskService crawlTaskService;
	
	private ExecutorService threadPool = null;
	
	private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
	
	public NewTaskReceiver(){
		threadPool = Executors.newCachedThreadPool(new ThreadFactory() {
			@Override
			public Thread newThread(Runnable r) {
				Thread thread = new Thread(r);
				thread.setDaemon(true);
				return thread;
			}
		});
	}
	/**
	 * 每隔30秒执行一次
	 * @throws InterruptedException
	 * @author ServerZhang
	 * @date 2017年7月5日
	 */
	@Scheduled(cron = "0/30 * * * * ?")  
	public void updateTask() throws InterruptedException{
		threadPool.execute(new Runnable(){
			@Override 
			public void run() { 
				crawlTaskService.updateTask4File();
			} 
		});            
	}
	
}
